package com.example.springbootrbac.Filter;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@WebFilter(filterName = "testFilter", urlPatterns = "/*",
        initParams = @WebInitParam(name = "noFilterUrl", value = "/login,/index,/tryLogout,/register,/jumpFilter"))
public class JumpFilter implements Filter {
    private List<String> noFilterUrls;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("启动过滤器");
        // 从过滤器配置中获取initParams参数
        String noFilterUrl = filterConfig.getInitParameter("noFilterUrl");
        // 将排除的URL放入成员变量noFilterUrls中
        if (StringUtils.isNotBlank(noFilterUrl)) {
            noFilterUrls = new ArrayList<>(Arrays.asList(noFilterUrl.split(",")));
        }
        System.out.println("noFilterUrls:"+noFilterUrls);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        System.out.println("过滤器处理中~~~~~~");
        // 若请求中包含noFilterUrls在则直接跳过过滤器进入下一步请求中,目前啥都不用做
        String url = ((HttpServletRequest)servletRequest).getRequestURI();
        System.out.println(url+"没有被拦截");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
